package com.tanhua.server.service;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.autoconfig.template.SmsTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.model.domain.User;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private SmsTemplate smsTemplate;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /*@Autowired
    private HuanXinTemplate huanXinTemplate;*/
    @DubboReference
    private UserApi userApi;
    @Autowired
    private UserFreezeService userFreezeService;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private MqMessageService mqMessageService;
    /**
     * 发送短信验证码方法
     *
     * @param phone
     */

    public void sendMsg(String phone) {
        User user = userApi.findByMobile(phone);
        System.err.println(user);
        if (user!=null){
            userFreezeService.checkUserStatus("1",user.getId());
        }
        //1.随机生成六位数字
//        String code = RandomStringUtils.randomNumeric(6);
        String code="123456";
        //2.调用template对象，发送手机短信
//        smsTemplate.sendSms(phone,code);
        //3.将验证码存入redis
        redisTemplate.opsForValue().set("CHECK_CODE_"+phone,code, Duration.ofMinutes(5));
    }

    /**
     * 验证登录
     * @param phone
     * @param code
     * @return
     */
    public Map loginVerification(String phone, String code) {
        //1.从Redis中获取下发的验证码
        String redisCode = redisTemplate.opsForValue().get("CHECK_CODE_" + phone);
        //2.对验证码进行校验（检验验证码是否存在，是否和输入的验证码一致）
        if (StringUtils.isEmpty(redisCode)||!redisCode.equals(code)){
            //验证码无效
            throw new BusinessException(ErrorResult.loginError());
        }
        //3.删除Redis中的验证码
        redisTemplate.delete("CHECK_CODE_" + phone);
        //4.通过手机号查询用户
        User user = userApi.findByMobile(phone);
        boolean isNew = false;
        //5.如果用户不存在，创建用户保存到数据库中
        String type="0101";//登录
        if (user==null){
            type="0102";//注册
            user=new User();
            user.setMobile(phone);
            user.setPassword(DigestUtil.md5Hex("123456"));
            Long userId=userApi.save(user);
            user.setId(userId);
            isNew=true;
           /* String hxUser="hx"+user.getId();
            Boolean create = huanXinTemplate.createUser(hxUser, Constants.INIT_PASSWORD);
            if (create){
                user.setHxUser(hxUser);
                user.setHxpassword(Constants.INIT_PASSWORD);
                userApi.update(user);
            }*/
        }
        mqMessageService.sendLogService(user.getId(),type,"user",null);
        //6.通过jwt生成token
        Map map=new HashMap();
        map.put("id",user.getId());
        map.put("mobile",phone);
        String token = JwtUtils.getToken(map);
        //7.构造返回值
        Map retMap=new HashMap();
        retMap.put("token",token);
        retMap.put("isNew",isNew);
        return retMap;
    }
}
